import zipfile
import os


def match_docx(filename, expression):
    ''' Checks whether the given docx file contains the given expression by extracting its
    contents via zipfile module and checking the appropriate .xml file within. 
    Returns True or False respectively '''

    a_file = os.path.abspath(filename)


    if zipfile.is_zipfile(a_file):
        input = zipfile.ZipFile(a_file, "r")
        
        try:
            xml_from_input = input.read("word/document.xml")
        except:
            print 'Not a proper .docx file'
            return False

        if expression.lower() in xml_from_input.lower():
            return True

    return False
